package callnumber.gtdev5.com.photoVault.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.bean.Gds;
import callnumber.gtdev5.com.photoVault.customview.MyOnItemClickListener;
import callnumber.gtdev5.com.photoVault.holder.DialogMemberHolder;

/**
 * Created by qp
 * PackageName APP_Lock
 * 2018/1/31 15:42
 */

public class DialogMemberAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<Gds> mList = new ArrayList<>();
    private int gid;
    private MyOnItemClickListener onItemClickListener;
    private int currentPosition;

    private int getcurrentPosition() {
        return currentPosition;
    }

    public DialogMemberAdapter(Context mContext, List<Gds> list) {
        this.mContext = mContext;
        this.mList = list;
        this.mInflater = LayoutInflater.from(mContext);
        currentPosition = mList.size() - 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        DialogMemberHolder holder = null;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_newmemer, parent, false);
            holder = new DialogMemberHolder(view);
            view.setTag(holder);
        } else {
            holder = (DialogMemberHolder) view.getTag();
        }
        if (view != null) {
            view.setOnClickListener(this);
        }

        return holder;
    }
    int i;
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof DialogMemberHolder) {
            Gds gds = mList.get(position);
            DialogMemberHolder dialogMemberHolder = (DialogMemberHolder) holder;
            dialogMemberHolder.tv_time.setText(gds.getName());
            dialogMemberHolder.tv_price.setText(gds.getPrice());
            if (position == currentPosition) {
//                dialogMemberHolder.linearLayout.setBackgroundResource(R.drawable.shape_vipdialog_gds_bg_red);
                dialogMemberHolder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.member_click_text));
                dialogMemberHolder.tv_price.setTextColor(ContextCompat.getColor(mContext, R.color.member_click_text));
                gid = gds.getGid();
            } else {
//                dialogMemberHolder.linearLayout.setBackgroundResource(R.drawable.shape_vipdialog_gds_bg_gre);
                dialogMemberHolder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.member_item_text));
                dialogMemberHolder.tv_price.setTextColor(ContextCompat.getColor(mContext, R.color.member_item_text));
            }
            dialogMemberHolder.itemView.setTag(position);
            dialogMemberHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    currentPosition = position;
                    notifyDataSetChanged();
                }
            });

            dialogMemberHolder.txt_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    i = getGid();
                }
            });
        }
    }

    public int getGid() {
        return gid;
    }

    @Override
    public int getItemCount() {
        return mList.size() > 0 ? mList.size() : 0;
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onItemcLick(v, (Integer) v.getTag());
        }
    }

    public void setOnItemClickListener(MyOnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
}
